package com.imooc.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.imooc.api.service.BaseService;
import com.imooc.article.mapper.CommentsMapper;
import com.imooc.article.mapper.CommentsMapperCustom;
import com.imooc.article.service.ArticlePortalService;
import com.imooc.article.service.CommentPortalService;
import com.imooc.exception.GraceException;
import com.imooc.grance.result.ResponseStatusEnum;
import com.imooc.pojo.Comments;
import com.imooc.pojo.vo.ArticleDetaiVO;
import com.imooc.pojo.vo.CommentsVO;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentPortalServiceImpl extends BaseService implements CommentPortalService {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private ArticlePortalService articlePortalService;
    @Autowired
    private CommentsMapperCustom commentsMapperCustom;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void createComment(String articleId,
                              String userId,
                              String nickName,
                              String face,
                              String fathCommentId,
                              String content) {
        ArticleDetaiVO articleDetaiVO = articlePortalService.getArticleDetaiVO(articleId);
        Comments comments = new Comments();
        comments.setId(sid.nextShort());
        comments.setCommentUserId(userId);
        comments.setArticleId(articleId);
        comments.setArticleCover(articleDetaiVO.getCover());
        comments.setCommentUserNickname(nickName);
        comments.setCommentUserFace(face);
        comments.setArticleTitle(articleDetaiVO.getTitle());
        comments.setContent(content);
        comments.setFatherId(fathCommentId);
        comments.setWriterId(articleDetaiVO.getPublishUserId());
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);

        redis.increment(REDIS_ARTICLE_COMMNET_COUNTS+":"+articleId, 1);
    }

    @Override
    public Integer getCommentCounts(String articleId) {
        String commentCountsRedis = redis.get(REDIS_ARTICLE_COMMNET_COUNTS + ":" + articleId);
        if (StringUtils.isBlank(commentCountsRedis)) {
            commentCountsRedis = "0";
        }
        return Integer.valueOf(commentCountsRedis);
    }

    @Override
    public PagedGridResult getCommentsList(String articleId,
                                           Integer page,
                                           Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("articleId", articleId);
        PageHelper.startPage(page, pageSize);
        List<CommentsVO> commentsVOList = commentsMapperCustom.queryComments(map);
        return pagedResultSettings(commentsVOList, page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PagedGridResult commnetMng(String writerId,
                                      Integer page,
                                      Integer pageSize) {
//        Comments comments = new Comments();
//        comments.setWriterId(writerId);
//        PageHelper.startPage(page, pageSize);
//        List<Comments> commentsList = commentsMapper.select(comments);
        Example example = new Example(Comments.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("writerId", writerId);
        List<Comments> commentsList = commentsMapper.selectByExample(example);
        return pagedResultSettings(commentsList, page);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteComments(String writerId,
                               String commentId) {
        Comments comments = new Comments();
        comments.setWriterId(writerId);
        comments.setId(commentId);
        int result = commentsMapper.delete(comments);
        if (result != 1) {
            GraceException.display(ResponseStatusEnum.DELETE_COMMENT_FAILED);
        }
    }
}
